using System.Text.RegularExpressions;

namespace Infrastructure.Utils
{
    public static class RegexPattern
    {
        /// <summary>
        /// 11位大陆手机号
        /// </summary>
        public const string Mobile = @"^1[1-9][0-9]{9}$";//宽松的手机验证,以应对运营商可能的新增号段。//较严格即目前开放号段@"^1[356789]\d{9}$"  \d会匹配全角数字
        public const string Email = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";//宽松邮箱
        public const string Email_Tradition = @"[a-zA-Z0-9][a-zA-Z0-9._-]*@[a-zA-Z0-9.-]+[.][a-zA-Z]+$";//传统邮箱 不包括 xx@zh.企业

        /// <summary>
        /// 18位大陆身份证
        /// </summary>
        public const string IdCard = @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|[Xx])$";//18位身份证,末尾Xx都行
        /// <summary>
        /// 车牌号
        /// </summary>
        public const string CarNumber = @"^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$";
        /// <summary>
        /// 自然数[即0和正整数：0,1,2...]
        /// </summary>
        public const string NaturalNumber = @"^[0-9]*$";
        /// <summary>
        /// 非负数【比如面积0,1.32,...】
        /// </summary>
        public const string NonnegativeNumber = @"^\d+(\.\d+)?$";
        /// <summary>
        /// 固定电话【03716731221，0371-67312214，4001234567，400-2222222】
        /// </summary>
        public const string FixedPhone = @"(^((0\d{2,3})-)?(\d{7,8})(-(\d{3,8}))?$)|(^400\d{7}$)";
        /// <summary>
        /// 使用正则匹配，默认非必填
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pattern"></param>
        /// <param name="isRequired">默认非必填</param>
        /// <returns></returns>
        public static bool IsMatch(string str, string pattern, bool isRequired = false)
        {
            if (string.IsNullOrWhiteSpace(str) && isRequired) return false;
            if (string.IsNullOrWhiteSpace(str)) return true;
            return Regex.IsMatch(str, pattern);
        }
        /// <summary>
        /// 手机号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobile(string str, bool isRequired = false)
        {
            return IsMatch(str, Mobile, isRequired);
        }
        /// <summary>
        /// 身份证号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIdCard(string str, bool isRequired = false)
        {
            return IsMatch(str, IdCard, isRequired);
        }
        /// <summary>
        /// 车牌号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsCarNumber(string str, bool isRequired = false)
        {
            return IsMatch(str, CarNumber, isRequired);
        }
        /// <summary>
        /// 自然数[即0和正整数：0,1,2...]
        /// </summary>
        /// <param name="str"></param>
        /// <param name="isRequired"></param>
        /// <returns></returns>
        public static bool IsNaturalNumber(string str, bool isRequired = false)
        {
            return IsMatch(str, NaturalNumber, isRequired);
        }
        /// <summary>
        /// 电话【手机号码，固定电话】
        /// </summary>
        /// <param name="str"></param>
        /// <param name="isRequired"></param>
        /// <returns></returns>
        public static bool IsPhone(string str, bool isRequired = false)
        {
            return IsMatch(str, FixedPhone, isRequired) && IsMatch(str, Mobile, isRequired);
        }
        public static bool IsEmail(string str, bool isRequired = false)
        {
            return IsMatch(str, Email_Tradition, isRequired);
        }
        public static bool IsNonnegativeNumber(string str, bool isRequired = false)
        {
            return IsMatch(str, NonnegativeNumber, isRequired);
        }
    }
}
